package com.indexingsystem.db.mongoDB.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.indexingsystem.boss.Vo.MagazinesDetailsVo;
import com.indexingsystem.boss.Vo.MagazinesQueryVo;
import com.indexingsystem.boss.Vo.MagazinesTableVo;
import com.indexingsystem.boss.Vo.ResCategoryRelationsVo;
import com.indexingsystem.boss.Vo.SpecialResourceDatasVo;
import com.indexingsystem.boss.entity.AuditLogs;
import com.indexingsystem.boss.entity.DhResourceauditlogs;
import com.indexingsystem.boss.entity.DhTodayStorageInfos;
import com.indexingsystem.boss.entity.Magazines;
import com.indexingsystem.boss.entity.ResCategoryRelations;
import com.indexingsystem.boss.entity.ResourceRelations;
import com.indexingsystem.boss.entity.Resources;
import com.indexingsystem.boss.entity.SpecialResourceDatas;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.boss.utils.JsonUtils;
import com.indexingsystem.db.mongoDB.MongoManager;
import com.indexingsystem.db.mongoDB.comon.MongoDBUtils;
import com.indexingsystem.db.mongoDB.comon.MongoTableName;
import com.indexingsystem.db.mongoDB.po.MagazinesListPo;
import com.indexingsystem.db.mongoDB.po.MagazinesResourceMongoVo;
import com.mongodb.BasicDBObject;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;

/**
 * 
 * @author Administrator 报刊缓存 集合名称：dh_magazines_list
 *         集合说明：对产品分类中产品列表信息进行冗余，便于在产品分类中获取产品信息避免多表连接。
 * 
 * 
 */
public class MagazinesMongo {
	public static void addMagazines(List<MagazinesResourceMongoVo> magazinesResourceMongoVoLsit, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "添加 ------->" + com.alibaba.fastjson.JSON.toJSONString( magazinesResourceMongoVoLsit, true ) );
		List<Document> addObjList = new ArrayList<Document>();
		if (null != magazinesResourceMongoVoLsit && magazinesResourceMongoVoLsit.size() > 0) {
			for (MagazinesResourceMongoVo magazinesResourceMongoVo : magazinesResourceMongoVoLsit) {
				Document addObj = new Document();

				addObj.put( "resId", magazinesResourceMongoVo.getResId() );
				addObj.put( "resTypeId", magazinesResourceMongoVo.getResTypeId() );
				addObj.put( "resTable", magazinesResourceMongoVo.getResTable() );
				addObj.put( "institutionId", institutionId );

				if (magazinesResourceMongoVo.getResources() != null) {
					Document resourcesDoc = MongoDBUtils.transform2Doc( magazinesResourceMongoVo.getResources() );
					addObj.put( "resources", resourcesDoc );
				}
				if (magazinesResourceMongoVo.getMagazines() != null) {
					Document magazinesDoc = MongoDBUtils.transform2Doc( magazinesResourceMongoVo.getMagazines() );
					addObj.put( "magazines", magazinesDoc );
				}

				if (magazinesResourceMongoVo.getAuditLogs() != null) {
					Document auditLogsDoc = MongoDBUtils.transform2Doc( magazinesResourceMongoVo.getAuditLogs() );
					addObj.put( "auditLogs", auditLogsDoc );
				}

				if (magazinesResourceMongoVo.getResourceauditlogs() != null) {
					Document resourceauditlogsdoc = MongoDBUtils.transform2Doc( magazinesResourceMongoVo.getResourceauditlogs() );
					addObj.put( "resourceauditlogs", resourceauditlogsdoc );
				}

				if (null != magazinesResourceMongoVo.getTodayStorageInfos()) {
					Document dhTodayStorageInfosDoc = MongoDBUtils.transform2Doc( magazinesResourceMongoVo.getTodayStorageInfos() );
					addObj.put( "todayStorageInfos", dhTodayStorageInfosDoc );
				}

				List<Document> specialResourceDatasDocList = new ArrayList<Document>();
				if (null != magazinesResourceMongoVo.getDhSpecialResourceDatasList()
						&& magazinesResourceMongoVo.getDhSpecialResourceDatasList().size() > 0) {
					for (SpecialResourceDatas dhSpecialResourceDatas : magazinesResourceMongoVo.getDhSpecialResourceDatasList()) {
						Document specialResourceDatasDoc = MongoDBUtils.transform2Doc( dhSpecialResourceDatas );
						specialResourceDatasDocList.add( specialResourceDatasDoc );
					}
				}
				addObj.put( "dhSpecialResourceDatasList", specialResourceDatasDocList );

				// 资源关系列表
				List<Document> dhResourceRelationsDocList = new ArrayList<Document>();
				if (null != magazinesResourceMongoVo.getDhResourceRelationsList() && magazinesResourceMongoVo.getDhResourceRelationsList().size() > 0) {
					for (ResourceRelations dhResourceRelations : magazinesResourceMongoVo.getDhResourceRelationsList()) {
						Document resourceRelationsDoc = MongoDBUtils.transform2Doc( dhResourceRelations );
						dhResourceRelationsDocList.add( resourceRelationsDoc );
					}
				}
				addObj.put( "dhResourceRelationsList", dhResourceRelationsDocList );

				// 资源分类关系列表
				List<Document> resCategoryRelationsList = new ArrayList<Document>();
				if (null != magazinesResourceMongoVo.getResCategoryRelationsList()
						&& magazinesResourceMongoVo.getResCategoryRelationsList().size() > 0) {
					for (ResCategoryRelations resCategoryRelations : magazinesResourceMongoVo.getResCategoryRelationsList()) {
						Document resCategoryRelationsVoDoc = MongoDBUtils.transform2Doc( resCategoryRelations );
						resCategoryRelationsList.add( resCategoryRelationsVoDoc );
					}
				}
				addObj.put( "resCategoryRelationsList", resCategoryRelationsList );

				List<Document> articlesList = new ArrayList<Document>();
				if (null != magazinesResourceMongoVo.getArticeIds() && magazinesResourceMongoVo.getArticeIds().size() > 0) {
					for (String resId : magazinesResourceMongoVo.getArticeIds()) {
						Document articlesDoc = MongoDBUtils.transform2Doc( resId );
						articlesList.add( articlesDoc );
					}
				}
				addObj.put( "articleIds", articlesList );

				if (!addObjList.contains( addObj )) {
					addObjList.add( addObj );
				}

			}

			if (null != addObjList && addObjList.size() > 0) {
				manager.addList( addObjList, MongoTableName.DH_MAGAZINES_LIST );
			}

		}
		// 向mongodb插入数据
	}

	public static void updateMagazinesByMagazinesReqVo(MagazinesResourceMongoVo magazinesListPo, String institutionId) {
		MongoManager manager = new MongoManager();

		Bson bson = null;
		bson = Filters.and( Filters.eq( "resId", magazinesListPo.getResId() ), Filters.eq( "institutionId", institutionId ) );
		Bson doc = null;

		doc = new Document( "$set", new Document( "magazines", magazinesListPo.getMagazines() )
				.append( "resCategoryRelationsList", magazinesListPo.getResCategoryRelationsList() )
				.append( "resources", magazinesListPo.getResources() )
				.append( "dhSpecialResourceDatasList", magazinesListPo.getDhSpecialResourceDatasList() )
				.append( "resourceauditlogs", magazinesListPo.getResourceauditlogs() )
				.append( "todayStorageInfos", magazinesListPo.getTodayStorageInfos() )

		);

		manager.updateOne( bson, doc, MongoTableName.DH_MAGAZINES_LIST );
	}

	public static void updateMagazines(MagazinesResourceMongoVo magazinesListPo, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "修改 ------->" + com.alibaba.fastjson.JSON.toJSONString( magazinesListPo, true ) );
		if (null != magazinesListPo) {
			Document mainDocument = new Document();
			mainDocument.put( "resId", magazinesListPo.getResId() );
			mainDocument.put( "resTypeId", magazinesListPo.getResTypeId() );
			mainDocument.put( "resTable", magazinesListPo.getResTable() );
			mainDocument.put( "institutionId", institutionId );

			Document magazinesDoc = MongoDBUtils.transform2Doc( magazinesListPo.getMagazines() );
			mainDocument.put( "magazines", magazinesDoc );

			Document resourcesDoc = MongoDBUtils.transform2Doc( magazinesListPo.getResources() );
			mainDocument.put( "resources", resourcesDoc );

			/*
			 * Document resourceauditlogsDoc = MongoDBUtils.transform2Doc(
			 * magazinesListPo.getResourceauditlogs() ); mainDocument.put(
			 * "resourceauditlogs", resourceauditlogsDoc );
			 */
			/*
			 * Document auditLogsDoc = MongoDBUtils.transform2Doc(
			 * magazinesListPo.getAuditLogs() ); mainDocument.put( "auditLogs",
			 * auditLogsDoc );
			 */
			/*
			 * Document todayStorageInfosDoc = MongoDBUtils.transform2Doc(
			 * magazinesListPo.getTodayStorageInfos() ); mainDocument.put(
			 * "todayStorageInfos", todayStorageInfosDoc );
			 */

			List<Document> specialResourceDatasDocList = new ArrayList<Document>();
			if (null != magazinesListPo.getDhSpecialResourceDatasList() && magazinesListPo.getDhSpecialResourceDatasList().size() > 0) {
				for (SpecialResourceDatas specialResourceDatas : magazinesListPo.getDhSpecialResourceDatasList()) {
					Document specialResourceDatasDoc = MongoDBUtils.transform2Doc( specialResourceDatas );
					specialResourceDatasDocList.add( specialResourceDatasDoc );
				}
			}
			mainDocument.put( "dhSpecialResourceDatasList", specialResourceDatasDocList );

			List<Document> dhResourceRelationsDocList = new ArrayList<Document>();
			if (null != magazinesListPo.getDhResourceRelationsList() && magazinesListPo.getDhResourceRelationsList().size() > 0) {
				for (ResourceRelations resourceRelations : magazinesListPo.getDhResourceRelationsList()) {
					Document resourceRelationsDoc = MongoDBUtils.transform2Doc( resourceRelations );
					dhResourceRelationsDocList.add( resourceRelationsDoc );
				}
			}
			mainDocument.put( "dhResourceRelationsList", dhResourceRelationsDocList );

			List<Document> articlesList = new ArrayList<Document>();
			if (null != magazinesListPo.getArticeIds() && magazinesListPo.getArticeIds().size() > 0) {
				for (String resId : magazinesListPo.getArticeIds()) {
					Document articlesDoc = MongoDBUtils.transform2Doc( resId );
					articlesList.add( articlesDoc );
				}
			}
			mainDocument.put( "articleIds", articlesList );

			UpdateOptions options = new UpdateOptions().upsert( true );
			Bson bson = Filters.and( Filters.eq( "resId", magazinesListPo.getResId() ), Filters.eq( "institutionId", institutionId ) );
			Document doc = new Document( "$set", mainDocument );
			manager.updateOne( bson, doc, options, MongoTableName.DH_MAGAZINES_LIST );

		}
	}

	public static void updateMagazines(Object o, String tag, String resId, String institutionId) {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "添加 ------->" + com.alibaba.fastjson.JSON.toJSONString( o, true ) );
		Bson bson = null;
		bson = Filters.and( Filters.eq( "resId", resId ), Filters.eq( "institutionId", institutionId ) );
		Bson doc = null;
		if ("Magazines".equals( tag )) {

			doc = new Document( "$set", new Document( "magazines", (List<Magazines>) o ) );
		} else if ("ResCategoryRelationsVo".equals( tag )) {

			doc = new Document( "$set", new Document( "resCategoryRelationsList", (List<ResCategoryRelationsVo>) o ) );
		} else if ("ResCategoryRelationsVo".equals( tag )) {

			doc = new Document( "$set", new Document( "resCategoryRelationsList", (List<ResCategoryRelationsVo>) o ) );
		} else if ("AuditLogs".equals( tag )) {

			doc = new Document( "$set", new Document( "auditLogsList", (List<AuditLogs>) o ) );
		} else if ("Resources".equals( tag )) {

			doc = new Document( "$set", new Document( "resources", (Resources) o ) );
		} else if ("SpecialResourceDatasVo".equals( tag )) {

			doc = new Document( "$set", new Document( "specialResourceDatasVoList", (List<SpecialResourceDatasVo>) o ) );
		} else if ("articleIds".equals( tag )) {

			doc = new Document( "$set", new Document( "articleIds", (List<String>) o ) );
		}
		manager.updateOne( bson, doc, MongoTableName.DH_MAGAZINES_LIST );
	}

	/**
	 * 批量删除
	 * 
	 * @param map
	 * @return
	 */
	public static void deleteMagazinesByIds(List<String> resIdList, String institutionId) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.in( "resId", resIdList ), Filters.eq( "institutionId", institutionId ) );
		manager.deleteAll( bson, MongoTableName.DH_MAGAZINES_LIST );
	}

	/**
	 * 返回报刊列表
	 * 
	 * @param map
	 * @return
	 */
	public static List<MagazinesTableVo> findMagazinesByPage(MagazinesQueryVo magazinesQueryVo, String institutionId) {
		MongoManager manager = new MongoManager();
		List<MagazinesTableVo> magazinesTableVoList = null;
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "查报刊列表 ------->" );
		BasicDBObject basicDBObject = new BasicDBObject();
		basicDBObject.put( "institutionId", institutionId );
		// 時間查詢
		if (magazinesQueryVo.getStartPublishDate() != null && !magazinesQueryVo.getStartPublishDate().equals( "" )) {

			BasicDBObject gt = new BasicDBObject( "$gte", magazinesQueryVo.getStartPublishDate() );
			basicDBObject.put( "resources.publishDate", gt );
		}
		if (magazinesQueryVo.getEndPublishDate() != null && !magazinesQueryVo.getEndPublishDate().equals( "" )) {
			BasicDBObject lt = new BasicDBObject( "$lte", magazinesQueryVo.getEndPublishDate() );
			basicDBObject.put( "resources.publishDate", lt );
		}

		if (magazinesQueryVo.getPeriodOfTime() != null && !magazinesQueryVo.getPeriodOfTime().equals( "" )) {

			basicDBObject.put( "magazines.periodOfTime", magazinesQueryVo.getPeriodOfTime() );
		}
		if (magazinesQueryVo.getStatus() != null) {

			basicDBObject.put( "resources.status", magazinesQueryVo.getStatus() );
		}
		if (null != magazinesQueryVo.getTerminalCode() && !magazinesQueryVo.getTerminalCode().equals( "" )) {

			basicDBObject.put( "dhSpecialResourceDatasList.terminalCode", magazinesQueryVo.getTerminalCode() );
		}
		// 模糊查詢
		if (null != magazinesQueryVo.getQueryCriteria() && !magazinesQueryVo.getQueryCriteria().equals( "" )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getQueryCriteria() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "resources.resName", pattern );
		}
		if (null != magazinesQueryVo.getAge() && !magazinesQueryVo.getAge().equals( "" )) {

			basicDBObject.put( "magazines.age", magazinesQueryVo.getAge() );
		}

		// 排序条件
		Document sortDocument = new Document();
		sortDocument.put( "resources.13", -1 );
		// 不显示的字段
		Document fetchFields = new Document();
		fetchFields.put( "_id", 0 );
		fetchFields.put( "resId", 0 );
		fetchFields.put( "resTable", 0 );
		fetchFields.put( "resTypeId", 0 );
		fetchFields.put( "magazines", 0 );
		fetchFields.put( "resCategoryRelationsList", 0 );
		fetchFields.put( "auditLogsList", 0 );
		fetchFields.put( "resources", 0 );
		fetchFields.put( "specialResourceDatasVoList", 0 );
		fetchFields.put( "todayStorageInfosList", 0 );
		fetchFields.put( "resourceauditlogs", 0 );

		List<Document> documentsList = manager.queryPages( MongoTableName.DH_MAGAZINES_LIST, basicDBObject, sortDocument, fetchFields,
				magazinesQueryVo.getPageNumber(), magazinesQueryVo.getPageSize() );
		try {
			if (null != documentsList && documentsList.size() > 0) {
				magazinesTableVoList = new ArrayList<MagazinesTableVo>();
				for (Document document : documentsList) {
					MagazinesTableVo magazinesTableVo = new MagazinesTableVo();
					Map<String, Object> map = (Map) document.get( "magazinesTableVo" );
					Iterator it = map.keySet().iterator();

					transMap2Bean2( map, magazinesTableVo );
					if (!magazinesTableVoList.contains( magazinesTableVo )) {
						magazinesTableVoList.add( magazinesTableVo );
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		/*
		 * LogCvt.debug( "查询 MagazinesTableVo 结果:" + JSON.toJSONString(
		 * magazinesTableVoList, true ) );
		 */
		return magazinesTableVoList;
	}

	public static List<MagazinesTableVo> findMagazinesByPageNew(MagazinesQueryVo magazinesQueryVo, String institutionId) {
		MongoManager manager = new MongoManager();
		List<MagazinesTableVo> magazinesTableVoList = null;
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "查图书列表 ------->" );
		BasicDBObject basicDBObject = new BasicDBObject();
		basicDBObject.put( "resTable", "Magazines" );
		basicDBObject.put( "institutionId", institutionId );

		if (StringUtils.isNotBlank( magazinesQueryVo.getQueryCriteria() )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getQueryCriteria() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "resources.resName", pattern );
		}
		if (null != magazinesQueryVo.getStatus()) {
			basicDBObject.put( "resources.status", magazinesQueryVo.getStatus() );
		}

		// 時間查詢
		if (magazinesQueryVo.getStartPublishDate() != null && !magazinesQueryVo.getStartPublishDate().equals( "" )) {

			BasicDBObject gt = new BasicDBObject( "$gte", magazinesQueryVo.getStartPublishDate() );
			basicDBObject.put( "resources.publishDate", gt );
		}
		if (magazinesQueryVo.getEndPublishDate() != null && !magazinesQueryVo.getEndPublishDate().equals( "" )) {
			BasicDBObject lt = new BasicDBObject( "$lte", magazinesQueryVo.getEndPublishDate() );
			basicDBObject.put( "resources.publishDate", lt );
		}
		if (magazinesQueryVo.getPeriodOfTime() != null && !magazinesQueryVo.getPeriodOfTime().equals( "" )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getPeriodOfTime() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "magazines.periodOfTime", pattern );
		}

		if (StringUtils.isNotBlank( magazinesQueryVo.getTerminalCode() )) {
			basicDBObject.put( "dhSpecialResourceDatasList.terminalCode", magazinesQueryVo.getTerminalCode() );
		}
		if (StringUtils.isNotBlank( magazinesQueryVo.getPublisher() )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getPublisher() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "magazines.publisher", pattern );
		}

		if (StringUtils.isNotBlank( magazinesQueryVo.getIssn() )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getIssn() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "magazines.issn", pattern );
		}

		if (StringUtils.isNotBlank( magazinesQueryVo.getAge() )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getAge() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "magazines.age", pattern );
		}

		// 排序条件
		Document sortDocument = new Document();
		sortDocument.put( "resources.publishDate", -1 );
		// 不显示的字段
		Document fetchFields = new Document();
		fetchFields.put( "_id", 0 );
		fetchFields.put( "magazines", 1 );
		fetchFields.put( "resources", 1 );

		List<Document> documentsList = manager.queryPages( MongoTableName.DH_MAGAZINES_LIST, basicDBObject, sortDocument, fetchFields,
				magazinesQueryVo.getPageNumber(), magazinesQueryVo.getPageSize() );
		try {
			if (null != documentsList && documentsList.size() > 0) {
				magazinesTableVoList = new ArrayList<MagazinesTableVo>();
				for (Document document : documentsList) {
					MagazinesTableVo magazinesTableVo = new MagazinesTableVo();

					Magazines magazines = new Magazines();
					magazines = (Magazines) MongoDBUtils.setFieldValue( magazines, Document.parse( JsonUtils.toJson( document.get( "magazines" ) ) ) );
					if (null != magazines) {
						magazinesTableVo.setAge( magazines.getAge() );
						magazinesTableVo.setAlias( magazines.getAlias() );
						magazinesTableVo.setPeriodOfTime( magazines.getPeriodOfTime() );
						magazinesTableVo.setIssn( magazines.getIsbn() );
						magazinesTableVo.setPageCount( magazines.getPageCount() );
						magazinesTableVo.setPublisher( magazines.getPublisher() );
						magazinesTableVo.setResId( magazines.getResId() );

					}

					Resources resources = new Resources();
					resources = (Resources) MongoDBUtils.setFieldValue( resources, Document.parse( JsonUtils.toJson( document.get( "resources" ) ) ) );
					if (null != resources) {
						magazinesTableVo.setPublishDate( resources.getPublishDate() );
						magazinesTableVo.setResName( resources.getResName() );
						magazinesTableVo.setStatus( resources.getStatus() );
						magazinesTableVo.setThumbnail( resources.getThumbnail() );

					}
					if (!magazinesTableVoList.contains( magazinesTableVo )) {
						magazinesTableVoList.add( magazinesTableVo );
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		LogCvt.debug( "查询 ArticlesTableVo 结果:" + JSON.toJSONString( magazinesTableVoList, true ) );
		return magazinesTableVoList;
	}

	public static MagazinesResourceMongoVo findMagazinesResourceMongoVoByRedId(String redId, String institutionId) {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "查图书列表 ------->" );
		BasicDBObject basicDBObject = new BasicDBObject();
		basicDBObject.put( "resTable", "Magazines" );
		basicDBObject.put( "institutionId", institutionId );

		if (StringUtils.isNotBlank( redId )) {
			basicDBObject.put( "resId", redId );
		}

		// 排序条件
		Document sortDocument = new Document();
		sortDocument.put( "resources.publishDate", -1 );
		// 不显示的字段
		Document fetchFields = new Document();
		fetchFields.put( "_id", 0 );
		List<Document> documentsList = manager.queryPages( MongoTableName.DH_MAGAZINES_LIST, basicDBObject, sortDocument, fetchFields, 1 );
		MagazinesResourceMongoVo magazinesResourceMongoVo = null;
		try {
			if (null != documentsList && documentsList.size() > 0) {
				for (Document document : documentsList) {

					magazinesResourceMongoVo = new MagazinesResourceMongoVo();

					String resId = JSON.toJSONString( document.get( "resId" ) );
					magazinesResourceMongoVo.setResId( resId );

					String resTable = JSON.toJSONString( document.get( "resTable" ) );
					magazinesResourceMongoVo.setResTable( resTable );

					String resTypeId = JSON.toJSONString( document.get( "resTypeId" ) );
					magazinesResourceMongoVo.setResTypeId( resTypeId );

					Magazines magazines = new Magazines();
					magazines = (Magazines) MongoDBUtils.setFieldValue( magazines, Document.parse( JsonUtils.toJson( document.get( "magazines" ) ) ) );
					if (null != magazines) {
						magazinesResourceMongoVo.setMagazines( magazines );

					}

					Resources resources = new Resources();
					resources = (Resources) MongoDBUtils.setFieldValue( resources, Document.parse( JsonUtils.toJson( document.get( "resources" ) ) ) );
					if (null != resources) {
						magazinesResourceMongoVo.setResources( resources );
					}

					DhResourceauditlogs resourceauditlogs = new DhResourceauditlogs();
					resourceauditlogs = (DhResourceauditlogs) MongoDBUtils.setFieldValue( resourceauditlogs,
							Document.parse( JsonUtils.toJson( document.get( "resourceauditlogs" ) ) ) );
					if (null != resourceauditlogs) {
						magazinesResourceMongoVo.setResourceauditlogs( resourceauditlogs );
					}

					AuditLogs auditLogs = new AuditLogs();
					auditLogs = (AuditLogs) MongoDBUtils.setFieldValue( auditLogs, Document.parse( JsonUtils.toJson( document.get( "auditLogs" ) ) ) );
					if (null != auditLogs) {
						magazinesResourceMongoVo.setAuditLogs( auditLogs );
					}

					DhTodayStorageInfos todayStorageInfos = new DhTodayStorageInfos();
					todayStorageInfos = (DhTodayStorageInfos) MongoDBUtils.setFieldValue( todayStorageInfos,
							Document.parse( JsonUtils.toJson( document.get( "todayStorageInfos" ) ) ) );
					if (null != todayStorageInfos) {
						magazinesResourceMongoVo.setTodayStorageInfos( todayStorageInfos );
					}

					List<SpecialResourceDatas> dhSpecialResourceDatasList = new ArrayList<SpecialResourceDatas>();
					dhSpecialResourceDatasList = JSONArray.parseArray( JsonUtils.toJson( document.get( "dhSpecialResourceDatasList" ) ),
							SpecialResourceDatas.class );
					if (null != dhSpecialResourceDatasList && dhSpecialResourceDatasList.size() > 0) {
						magazinesResourceMongoVo.setDhSpecialResourceDatasList( dhSpecialResourceDatasList );
					}

					List<ResourceRelations> dhResourceRelationsList = new ArrayList<ResourceRelations>();
					dhResourceRelationsList = JSONArray.parseArray( JsonUtils.toJson( document.get( "dhResourceRelationsList" ) ),
							ResourceRelations.class );
					if (null != dhResourceRelationsList && dhResourceRelationsList.size() > 0) {
						magazinesResourceMongoVo.setDhResourceRelationsList( dhResourceRelationsList );
					}

					List<ResCategoryRelations> resCategoryRelationsList = new ArrayList<ResCategoryRelations>();
					resCategoryRelationsList = JSONArray.parseArray( JsonUtils.toJson( document.get( "resCategoryRelationsList" ) ),
							ResCategoryRelations.class );
					if (null != resCategoryRelationsList && resCategoryRelationsList.size() > 0) {
						magazinesResourceMongoVo.setResCategoryRelationsList( resCategoryRelationsList );
					}

					List<String> articeIds = new ArrayList<String>();
					articeIds = JSONArray.parseArray( JsonUtils.toJson( document.get( "articleIds" ) ), String.class );
					if (null != articeIds) {
						magazinesResourceMongoVo.setArticeIds( articeIds );
					}

				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		LogCvt.debug( "查询 magazinesResourceMongoVo 结果:" + JSON.toJSONString( magazinesResourceMongoVo, true ) );
		return magazinesResourceMongoVo;
	}

	/**
	 * 通过资源idlist 和机构id批量查询mongo期刊资源.
	 * 
	 * @param resIdList
	 * @param institutionId
	 * @return
	 */
	public static List<MagazinesResourceMongoVo> findMagazinesResourceMongoVoListByRedId(List<String> resIdList, String institutionId) {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "查图书列表 ------->" );
		BasicDBObject basicDBObject = new BasicDBObject();
		basicDBObject.put( "resTable", "Magazines" );
		basicDBObject.put( "institutionId", institutionId );

		if (null != resIdList && resIdList.size() > 0) {
			basicDBObject.put( "resId", new BasicDBObject( "$in", resIdList ) );
		}

		// 排序条件
		Document sortDocument = new Document();
		sortDocument.put( "resources.publishDate", -1 );
		// 不显示的字段
		Document fetchFields = new Document();
		fetchFields.put( "_id", 0 );
		List<Document> documentsList = manager.queryPages( MongoTableName.DH_MAGAZINES_LIST, basicDBObject, sortDocument, fetchFields, 1 );
		List<MagazinesResourceMongoVo> magazinesResMongoVoList = null;

		try {
			if (null != documentsList && documentsList.size() > 0) {
				magazinesResMongoVoList = new ArrayList<MagazinesResourceMongoVo>();
				for (Document document : documentsList) {
					MagazinesResourceMongoVo magazinesResourceMongoVo = new MagazinesResourceMongoVo();

					String resId = JSON.toJSONString( document.get( "resId" ) );
					magazinesResourceMongoVo.setResId( resId );

					String resTable = JSON.toJSONString( document.get( "resTable" ) );
					magazinesResourceMongoVo.setResTable( resTable );

					String resTypeId = JSON.toJSONString( document.get( "resTypeId" ) );
					magazinesResourceMongoVo.setResTypeId( resTypeId );

					Magazines magazines = new Magazines();
					magazines = (Magazines) MongoDBUtils.setFieldValue( magazines, Document.parse( JsonUtils.toJson( document.get( "magazines" ) ) ) );
					if (null != magazines) {
						magazinesResourceMongoVo.setMagazines( magazines );

					}

					Resources resources = new Resources();
					resources = (Resources) MongoDBUtils.setFieldValue( resources, Document.parse( JsonUtils.toJson( document.get( "resources" ) ) ) );
					if (null != resources) {
						magazinesResourceMongoVo.setResources( resources );
					}

					DhResourceauditlogs resourceauditlogs = new DhResourceauditlogs();
					resourceauditlogs = (DhResourceauditlogs) MongoDBUtils.setFieldValue( resourceauditlogs,
							Document.parse( JsonUtils.toJson( document.get( "resourceauditlogs" ) ) ) );
					if (null != resourceauditlogs) {
						magazinesResourceMongoVo.setResourceauditlogs( resourceauditlogs );
					}

					AuditLogs auditLogs = new AuditLogs();
					auditLogs = (AuditLogs) MongoDBUtils.setFieldValue( auditLogs, Document.parse( JsonUtils.toJson( document.get( "auditLogs" ) ) ) );
					if (null != auditLogs) {
						magazinesResourceMongoVo.setAuditLogs( auditLogs );
					}

					DhTodayStorageInfos todayStorageInfos = new DhTodayStorageInfos();
					todayStorageInfos = (DhTodayStorageInfos) MongoDBUtils.setFieldValue( todayStorageInfos,
							Document.parse( JsonUtils.toJson( document.get( "todayStorageInfos" ) ) ) );
					if (null != todayStorageInfos) {
						magazinesResourceMongoVo.setTodayStorageInfos( todayStorageInfos );
					}

					List<SpecialResourceDatas> dhSpecialResourceDatasList = new ArrayList<SpecialResourceDatas>();
					dhSpecialResourceDatasList = JSONArray.parseArray( JsonUtils.toJson( document.get( "dhSpecialResourceDatasList" ) ),
							SpecialResourceDatas.class );
					if (null != dhSpecialResourceDatasList && dhSpecialResourceDatasList.size() > 0) {
						magazinesResourceMongoVo.setDhSpecialResourceDatasList( dhSpecialResourceDatasList );
					}

					List<ResourceRelations> dhResourceRelationsList = new ArrayList<ResourceRelations>();
					dhResourceRelationsList = JSONArray.parseArray( JsonUtils.toJson( document.get( "dhResourceRelationsList" ) ),
							ResourceRelations.class );
					if (null != dhResourceRelationsList && dhResourceRelationsList.size() > 0) {
						magazinesResourceMongoVo.setDhResourceRelationsList( dhResourceRelationsList );
					}

					List<ResCategoryRelations> resCategoryRelationsList = new ArrayList<ResCategoryRelations>();
					resCategoryRelationsList = JSONArray.parseArray( JsonUtils.toJson( document.get( "resCategoryRelationsList" ) ),
							ResCategoryRelations.class );
					if (null != resCategoryRelationsList && resCategoryRelationsList.size() > 0) {
						magazinesResourceMongoVo.setResCategoryRelationsList( resCategoryRelationsList );
					}

					List<String> articeIds = new ArrayList<String>();
					articeIds = JSONArray.parseArray( JsonUtils.toJson( document.get( "articleIds" ) ), String.class );
					if (null != articeIds) {
						magazinesResourceMongoVo.setArticeIds( articeIds );
					}

					if (!magazinesResMongoVoList.contains( magazinesResourceMongoVo )) {
						magazinesResMongoVoList.add( magazinesResourceMongoVo );
					}

				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		LogCvt.debug( "查询 magazinesResourceMongoVo 结果:" + JSON.toJSONString( magazinesResMongoVoList, true ) );
		return magazinesResMongoVoList;
	}

	public static Integer findMagazinesMaxCount(MagazinesQueryVo magazinesQueryVo, String institutionId) {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "查报刊列表 ------->" );
		BasicDBObject basicDBObject = new BasicDBObject();
		basicDBObject.put( "resTable", "Magazines" );
		basicDBObject.put( "institutionId", institutionId );

		if (StringUtils.isNotBlank( magazinesQueryVo.getQueryCriteria() )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getQueryCriteria() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "resources.resName", pattern );
		}
		if (null != magazinesQueryVo.getStatus()) {
			basicDBObject.put( "resources.status", magazinesQueryVo.getStatus() );
		}

		// 時間查詢
		if (magazinesQueryVo.getStartPublishDate() != null && !magazinesQueryVo.getStartPublishDate().equals( "" )) {

			BasicDBObject gt = new BasicDBObject( "$gte", magazinesQueryVo.getStartPublishDate() );
			basicDBObject.put( "resources.publishDate", gt );
		}
		if (magazinesQueryVo.getEndPublishDate() != null && !magazinesQueryVo.getEndPublishDate().equals( "" )) {
			BasicDBObject lt = new BasicDBObject( "$lte", magazinesQueryVo.getEndPublishDate() );
			basicDBObject.put( "resources.publishDate", lt );
		}
		if (magazinesQueryVo.getPeriodOfTime() != null && !magazinesQueryVo.getPeriodOfTime().equals( "" )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getPeriodOfTime() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "magazines.periodOfTime", pattern );
		}

		if (StringUtils.isNotBlank( magazinesQueryVo.getTerminalCode() )) {
			basicDBObject.put( "dhSpecialResourceDatasList.terminalCode", magazinesQueryVo.getTerminalCode() );
		}
		if (StringUtils.isNotBlank( magazinesQueryVo.getPublisher() )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getPublisher() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "magazines.publisher", pattern );
		}

		if (StringUtils.isNotBlank( magazinesQueryVo.getIssn() )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getIssn() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "magazines.issn", pattern );
		}

		if (StringUtils.isNotBlank( magazinesQueryVo.getAge() )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getAge() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "magazines.age", pattern );
		}

		// 不显示的字段
		Document fetchFields = new Document();
		fetchFields.put( "_id", 0 );
		fetchFields.put( "resId", 1 );
		Integer maxCount = manager.queryAggregateListMaxCount( basicDBObject, MongoTableName.DH_MAGAZINES_LIST );

		LogCvt.debug( "查询 findArticlesMaxCount 总数量:" + maxCount );
		return maxCount;
	}

	/**
	 * 返回报刊列表
	 * 
	 * @param map
	 * @return
	 */
	public static List<MagazinesTableVo> findMagazinesByPageCount(MagazinesQueryVo magazinesQueryVo, String institutionId) {
		MongoManager manager = new MongoManager();
		List<MagazinesTableVo> magazinesTableVoList = null;
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "查报刊列表 ------->" );

		BasicDBObject basicDBObject = new BasicDBObject();
		basicDBObject.put( "institutionId", institutionId );

		// 時間查詢
		if (magazinesQueryVo.getStartPublishDate() != null && !magazinesQueryVo.getStartPublishDate().equals( "" )) {
			basicDBObject.put( "magazinesTableVo.publishDate", new BasicDBObject().put( "$gte", magazinesQueryVo.getStartPublishDate() ) );
			BasicDBObject gt = new BasicDBObject( "$gte", magazinesQueryVo.getStartPublishDate() );
			basicDBObject.put( "magazinesTableVo.publishDate", gt );
		}
		if (magazinesQueryVo.getEndPublishDate() != null && !magazinesQueryVo.getEndPublishDate().equals( "" )) {
			BasicDBObject lt = new BasicDBObject( "$lte", magazinesQueryVo.getEndPublishDate() );
			basicDBObject.put( "magazinesTableVo.publishDate", lt );
		}
		if (magazinesQueryVo.getPeriodOfTime() != null && !magazinesQueryVo.getPeriodOfTime().equals( "" )) {

			basicDBObject.put( "magazinesTableVo.periodOfTime", magazinesQueryVo.getPeriodOfTime() );
		}
		if (magazinesQueryVo.getStatus() != null) {

			basicDBObject.put( "magazinesTableVo.status", magazinesQueryVo.getStatus() );
		}
		if (null != magazinesQueryVo.getTerminalCode() && !magazinesQueryVo.getTerminalCode().equals( "" )) {

			basicDBObject.put( "magazinesTableVo.terminalCode", magazinesQueryVo.getTerminalCode() );
		}
		// 模糊查詢
		if (null != magazinesQueryVo.getQueryCriteria() && !magazinesQueryVo.getQueryCriteria().equals( "" )) {
			Pattern pattern = Pattern.compile( ".*" + magazinesQueryVo.getQueryCriteria() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "magazinesTableVo.resName", pattern );
		}
		if (null != magazinesQueryVo.getAge() && !magazinesQueryVo.getAge().equals( "" )) {

			basicDBObject.put( "magazinesTableVo.age", magazinesQueryVo.getAge() );
		}

		// 排序条件
		Document sortDocument = new Document();
		sortDocument.put( "magazinesTableVo.15", -1 );
		// 不显示的字段
		Document fetchFields = new Document();
		fetchFields.put( "_id", 0 );
		fetchFields.put( "resId", 0 );
		fetchFields.put( "resTable", 0 );
		fetchFields.put( "resTypeId", 0 );
		fetchFields.put( "magazines", 0 );
		fetchFields.put( "resCategoryRelationsList", 0 );
		fetchFields.put( "auditLogsList", 0 );
		fetchFields.put( "resources", 0 );
		fetchFields.put( "specialResourceDatasVoList", 0 );
		fetchFields.put( "todayStorageInfosList", 0 );
		fetchFields.put( "resourceauditlogs", 0 );
		List<Document> documentsList = manager.queryPages( MongoTableName.DH_MAGAZINES_LIST, basicDBObject, sortDocument, fetchFields,
				magazinesQueryVo.getPageNumber() );
		try {
			if (null != documentsList && documentsList.size() > 0) {
				magazinesTableVoList = new ArrayList<MagazinesTableVo>();
				for (Document document : documentsList) {
					MagazinesTableVo magazinesTableVo = new MagazinesTableVo();
					Map<String, Object> map = (Map) document.get( "magazinesTableVo" );
					Iterator it = map.keySet().iterator();

					transMap2Bean2( map, magazinesTableVo );
					if (!magazinesTableVoList.contains( magazinesTableVo )) {
						magazinesTableVoList.add( magazinesTableVo );
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		/*
		 * LogCvt.debug( "查询 MagazinesTableVo 结果:" + JSON.toJSONString(
		 * magazinesTableVoList, true ) );
		 */
		return magazinesTableVoList;
	}

	public static void transMap2Bean2(Map<String, Object> map, Object obj) {
		if (map == null || obj == null) {
			return;
		}
		try {
			BeanUtils.populate( obj, map );
		} catch (Exception e) {
			System.out.println( "transMap2Bean2 Error " + e );
		}

	}

	/**
	 * 返回报刊详情
	 * 
	 * @param map
	 * @return
	 */
	public static MagazinesDetailsVo findMagazinesDetailByToMagazinesDetailsVo(String resId, String institutionId) {
		MongoManager manager = new MongoManager();
		MagazinesDetailsVo magazinesDetailsVo = null;
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "查报刊详情 ------->" );

		Bson bson = Filters.and( Filters.eq( "resId", resId ), Filters.eq( "institutionId", institutionId ) );
		// 排序条件
		Document sortDocument = new Document();
		sortDocument.put( "publishDate", -1 );

		List<Document> documentsList = manager.queryPages( MongoTableName.DH_MAGAZINES_LIST, bson, sortDocument, null, 0, 1 );
		try {
			if (null != documentsList && documentsList.size() > 0) {
				magazinesDetailsVo = new MagazinesDetailsVo();
				for (Document document : documentsList) {
					MagazinesListPo magazinesListPo = new MagazinesListPo();
					magazinesListPo = (MagazinesListPo) MongoDBUtils.setFieldValue( magazinesListPo, document );
					if (null != magazinesListPo) {
						List<AuditLogs> auditLogsList = new ArrayList<AuditLogs>();
						auditLogsList.add( magazinesListPo.getAuditLogs() );
						magazinesDetailsVo.setAuditLogsList( auditLogsList );
						magazinesDetailsVo.setResCategoryRelationsList( magazinesListPo.getResCategoryRelationsList() );
						magazinesDetailsVo.setMagazines( magazinesListPo.getMagazines() );
						magazinesDetailsVo.setResources( magazinesListPo.getResources() );
						magazinesDetailsVo.setSpecialResourceDatasList( magazinesListPo.getDhSpecialResourceDatasList() );
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		/*
		 * LogCvt.debug( "查询 magazinesDetailsVo  结果:" + JSON.toJSONString(
		 * magazinesDetailsVo, true ) );
		 */
		return magazinesDetailsVo;
	}

	/**
	 * 返回报刊详情
	 * 
	 * @param map
	 * @return
	 */
	public static MagazinesTableVo findMagazinesDetailByToMagazinesTableVo(String resId, String institutionId) {
		MongoManager manager = new MongoManager();
		MagazinesTableVo magazinesTableVo = null;
		LogCvt.debug( MongoTableName.DH_MAGAZINES_LIST + "查报刊详情 ------->" );

		Bson bson = Filters.and( Filters.eq( "resId", resId ), Filters.eq( "institutionId", institutionId ) );
		// 排序条件
		Document sortDocument = new Document();
		sortDocument.put( "publishDate", -1 );

		List<Document> documentsList = manager.queryPages( MongoTableName.DH_MAGAZINES_LIST, bson, sortDocument, null, 0, 1 );
		try {
			if (null != documentsList && documentsList.size() > 0) {
				magazinesTableVo = new MagazinesTableVo();
				for (Document document : documentsList) {
					MagazinesListPo magazinesListPo = new MagazinesListPo();
					Set yy = document.keySet();
					magazinesListPo = (MagazinesListPo) MongoDBUtils.setFieldValue( magazinesListPo, document );
					if (null != magazinesListPo) {
						magazinesTableVo = magazinesListPo.getMagazinesTableVo();

					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		/*
		 * LogCvt.debug( "查询 magazinesDetailsVo  结果:" + JSON.toJSONString(
		 * magazinesTableVo, true ) );
		 */
		return magazinesTableVo;
	}
}